package com.etc.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.etc.dto.Result;
import com.etc.entity.Games;
import com.etc.entity.PageInfo;
import com.etc.service.AdminService;
import com.etc.service.impl.AdminServiceImpl;
import com.etc.service.impl.GamesServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;


/**
 * Servlet implementation class GamesServlet
 */
@WebServlet("/GamesServlet")
public class GamesServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private AdminService adminService = new AdminServiceImpl();
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GamesServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String op = request.getParameter("op");
		ObjectMapper objectMapper = new ObjectMapper();
		String json = "";
		Result result = new Result();
		if("newestgames".equals(op)) {
			List<Games> topFourGamesList = new GamesServiceImpl().topFourGames();
			if(topFourGamesList!=null) {
				result.ok(topFourGamesList);
			}else {
				result.fail(0, "查无数据");
			}
			json = objectMapper.writeValueAsString(result);
		}else if ("recommendgame".equals(op)) {
			List<Games> topSixGamesList = new GamesServiceImpl().topSixGames();
			if(topSixGamesList!=null) {
				result.ok(topSixGamesList);
			}else {
				result.fail(0, "查无数据");
			}
			json = objectMapper.writeValueAsString(result);
		}else if ("find".equals(op)) {
			String gamesId = request.getParameter("gamesId");
			Games games = new GamesServiceImpl().findGamesByGamesId(Integer.valueOf(gamesId));
			if(games!=null) {
				result.ok(games);
			}else {
				result.fail(0, "查无数据");
			}
			json = objectMapper.writeValueAsString(result);
		}else if ("findAll".equals(op)) {
			List<Games> findAllGames = new GamesServiceImpl().findAllGames();
			if(findAllGames!=null) {
				result.ok(findAllGames);
			}else {
				result.fail(0, "查无数据");
			}
			json = objectMapper.writeValueAsString(result);

		}else if ("findAllGamesByCondition".equals(op)) {
			String typeId = request.getParameter("typeId");
			String minPrice = request.getParameter("minPrice");
			String maxPrice = request.getParameter("maxPrice");
			String gamesName = request.getParameter("gamesName");
			
			Integer t = null;
			BigDecimal p1 = null;
			BigDecimal p2 = null;
			if(typeId!=null&&!typeId.isEmpty()) {
				t = Integer.valueOf(typeId);
			}
			if(minPrice!=null&&!minPrice.isEmpty()&&!"不限".equals(minPrice)) {
				p1 = new BigDecimal(minPrice);
			}
			if(maxPrice!=null&&!maxPrice.isEmpty()) {
				p2 = new BigDecimal(maxPrice);
			}
			if("".equals(gamesName)) {
				gamesName=null;
			}
			List<Games> findAllGamesByCondition = new GamesServiceImpl().findAllGamesByCondition(t, p1, p2, gamesName);
			if(findAllGamesByCondition!=null) {
				result.ok(findAllGamesByCondition);
			}else {
				result.fail(0, "查无数据");
			}
			json = objectMapper.writeValueAsString(result);
			
		}else if ("findByPage".equals(op)) {
			
			String typeId = request.getParameter("typeId");
			String minPrice = request.getParameter("minPrice");
			BigDecimal min = null;
			BigDecimal max = null;
			if("".equals(minPrice)) {
				minPrice=null;
			}else {
				min = new BigDecimal(minPrice);
			}
			String maxPrice = request.getParameter("maxPrice");
			if(maxPrice!=null) {
				max = new BigDecimal(maxPrice);
			}
			String gamesName = request.getParameter("gamesName");
			String pageNum = request.getParameter("pageNum");
			String pageSize = request.getParameter("pageSize");
			
			if (pageNum == null || pageSize == null) {
				result.fail(0, "参数错误");
			}else {
				// 调用业务层
				PageInfo<Games> pageInfo =  new GamesServiceImpl().getGoodsListByPage(Integer.valueOf(typeId), min, max, gamesName, Integer.parseInt(pageNum), Integer.parseInt(pageSize));
				result.ok(pageInfo);
			}
			json = objectMapper.writeValueAsString(result);

			
		}else if ("populargames".equals(op)) {
			List<Games> topFiveGamesList = new GamesServiceImpl().topFiveGames();
			if(topFiveGamesList!=null) {
				result.ok(topFiveGamesList);
			}else {
				result.fail(0, "查无数据");
			}
			json = objectMapper.writeValueAsString(result);
		}
		
		if("findGamesByPage".equals(op)) {// 姜南

			//接受请求参数
			String nickNameKeyWord = request.getParameter("nickNameKeyWord");
			String pageNum = request.getParameter("pageNum");
			String pageSize = request.getParameter("pageSize");
			
			//对参数进行校验
			if(pageNum==null || pageSize==null) {
				result.fail(0, "参数错误");
			}else {
				//调用业务层
				PageInfo<Games> pageInfo = adminService.getGamesListByPage(nickNameKeyWord, Integer.parseInt(pageNum), Integer.parseInt(pageSize));
				result.ok(pageInfo);
			}
			
			
			json = objectMapper.writeValueAsString(result);
			
		}else if ("updateStatus".equals(op)) {
			// 接收参数
			String gamesId = request.getParameter("gamesId");
			String status = request.getParameter("status");

			// 对参数进行效验
			if (gamesId == null || status == null) {
				result.fail(0, "参数错误");
			} else {
				// 调用业务,更新状态
				boolean flag = adminService.updateGamesStatus(Integer.parseInt(status),Integer.parseInt(gamesId));
				if (flag) {
					result.ok(flag);
				} else {
					result.fail(0, "更新状态失败");
				}
			}
			
			json = objectMapper.writeValueAsString(result);
			
			
		}
		
		
		
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json;charset=utf-8");
		response.getWriter().write(json);
	}
	
	

	
	
}